//
//  HttpHeaders.m
//  HSEDU
//
//  Created by oceans on 2018/7/13.
//  Copyright © 2018年 xvv. All rights reserved.
//

#import "HttpHeaders.h"
#import "Utility.h"
#import "NSString+sha256.h"

@implementation HttpHeaders

+ (NSDictionary *)getTokenHttpHeaders {
    
    NSString *appid = @"20190628153658";
    NSString *secret = @"FNLoWbLDnTsT4sCCqdpLCcPwp4LfHh4L";
    
    NSString *nonce = [Utility randLetters:32 onlyLower:YES];
    
    NSString *timestamp = [NSString stringWithFormat:@"%.0f", [[NSDate date] timeIntervalSince1970]];
    NSString *sn = [Utility uuid];
    NSString *os = @"ios";
    NSString *build = @"2";//[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
    NSString *install = @"0";
    
    NSDictionary *paras = @{
                            @"appid":appid,
                            @"os":os,
                            @"build":build,
                            @"timestamp":timestamp,
                            @"sn":sn,
                            @"install":install
                            };
    
    NSArray *keys = [paras.allKeys sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        return [obj1 compare:obj2 options:NSNumericSearch];
    }];
    NSMutableString *paraString = [NSMutableString string];
    for (NSString *key in keys) {
        [paraString appendString:[paras objectForKey:key]];
    }
    
    NSString *sign = [[NSString stringWithFormat:@"%@%@%@", secret, paraString, nonce] sha256];
    
    NSMutableDictionary *finalDic = [NSMutableDictionary dictionaryWithDictionary:paras];
    [finalDic setObject:sign forKey:@"sign"];
    [finalDic setObject:nonce forKey:@"nonce"];
    
    return finalDic;
}

+ (NSDictionary *)getHttpHeaders:(NSDictionary *)dic {
    
    NSString *appid = @"20190628153658";
    NSString *secret = @"FNLoWbLDnTsT4sCCqdpLCcPwp4LfHh4L";
    
    NSString *timestamp = [NSString stringWithFormat:@"%.0f", [[NSDate date] timeIntervalSince1970]];
    NSString *sn = [Utility uuid];
    
    if (dic == nil) {
        dic = @{};
    }
    
    NSMutableDictionary *paras = [NSMutableDictionary dictionaryWithDictionary:dic];
    [paras setObject:appid forKey:@"app_id"];
    [paras setObject:timestamp forKey:@"timestamps"];
    [paras setObject:sn forKey:@"device_id"];
    
    NSArray *keys = [paras.allKeys sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        return [obj1 compare:obj2 options:NSNumericSearch];
    }];
    NSMutableString *paraString = [NSMutableString string];
    for (NSString *key in keys) {
        [paraString appendString:key];
        [paraString appendString:[paras objectForKey:key]];
    }
    
    [paraString insertString:secret atIndex:0];
    
    NSData *data = [paraString dataUsingEncoding:NSUTF8StringEncoding];
    
    NSString *sign = [data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
    
    sign = [Utility md5:sign];
    
    sign = [sign uppercaseString];
    
    NSMutableDictionary *finalDic = [NSMutableDictionary dictionaryWithDictionary:paras];
    [finalDic setObject:sign forKey:@"sign"];
    
    return finalDic;
}

@end
